const Service = require('egg').Service;

class LikeService extends Service {
    get Like() {
        return this.app.model.Like;
    }
    async setlike(videoinfo,islike) {
        const {userid,videoid} = videoinfo
        const likemodel = new this.Like({userid,videoid,like:islike})
        const dbback = await likemodel.save()
        return dbback
    }

    async updatelike(videoid,islike) {
        const dbback = await this.Like.findByIdAndUpdate(videoid,{like:islike})
        return dbback
    }

    async getlike(videoid,userid){
        return await this.Like.findOne({videoid,userid})
    }

    async list(userid,page,pagesize){
        const skip = (page-1)*pagesize
        const likes = await this.Like.find({userid}).skip(skip).limit(pagesize).sort({_id:-1}).populate('videoid','title likecount')
        const count = await this.Like.countDocuments()
        return {likes,count}
    }

}

module.exports = LikeService;